﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class NavBarController : BaseController
    {
        //
        // GET: /Admin/NagBar/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AjaxGetList()
        {
            NavBarModel model = new NavBarModel();
            var data = model.GetNavList();
            return Content(SerializeJson(data));
        }

        public ActionResult AddNavBar()
        {
            NavBarModel model = new NavBarModel();
            model.Init();
            model.ParentID = Guid.Empty.ToString();
            return View(model);
        }
        [HttpPost]
        public ActionResult AddNavBar(NavBarModel model)
        {
            if (ModelState.IsValid)
            {
                model.Add();
                return Content("<script>parent.SubmitSuccess()</script>");
            }
            else
            {
                return View(model);
            }
        }

        public ActionResult EditNavBar(string id)
        {
            NavBarModel model = new NavBarModel();
            model.LoadData(id);
            model.Init();
            return View(model);
        }
        [HttpPost]
        public ActionResult EditNavBar(NavBarModel model)
        {
            if (ModelState.IsValid)
            {
                model.Update();
                return Content("<script>parent.SubmitSuccess()</script>");
            }
            else
            {
                model.Init();
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult DelNavBar(string id)
        {
            NavBarModel model = new NavBarModel();
            if (model.HasLeafBavBar(id))
            {
                return Content(SerializeJson(new { result = false, msg = "请先删除子节点！" }));
            }
            else
            {
                model.Delete(id);
                return Content(SerializeJson(new { result = true, msg = "删除成功！" }));
            }
        }

    }
}
